<?php

namespace App\Repositories\Eloquent;

use App\Models\RuleSet;
use App\Repositories\Contracts\RuleSetInterface;
use MakeRep\Repository;

class RuleSetRepository extends Repository implements RuleSetInterface
{

    function model()
    {
        return RuleSet::class;
    }

    /**
     * 添加返利设置
     * @param $data
     * @return void
     */
    public function create($data,$policyId){

        if (isset($data['rule_type'])) {

            $this->delete(['policy_id'=>$policyId]);

            $rebate = [];
            $time = date('Y-m-d H:i:s');
            foreach ($data['rule_type'] as $key=>$value) {

                $item = [
                    'policy_id'     =>$policyId,
                    'rule_type'     => $value,
                    'rebate_price'  => $data['rebate_price'][$key],
                    'rebate_percentage' => $data['rebate_percentage'][$key],
                    'biding_price'  => $data['biding_price'][$key],
                    'biding_percentage' => $data['biding_percentage'][$key],
                    'freight_min'   => $data['freight_min'][$key],
                    'freight_max'   => $data['freight_max'][$key],
                    'created_at'    => $time,
                    'updated_at'    => $time,
                ];

                $rebate[] = $item;
            }

            if ($rebate) {

                $this->insert($rebate);
            }
        }

    }
}